// pages/payment/payment.js
import RunData from "../../config/runData";
import getRegisterCardList from "../../api/getRegisterCardList";
import getPrenosInfo from "../../api/getPrenosInfo";

Page({
    /**
     * 页面的初始数据
     */
    data: {
        hos: undefined,
        choiced: -1,
        choicedHos: "请选择医院",
        choiceCardDisable:true,
        cards:undefined,
        choicedCardIndex:-1,
        choicedCardTxt:"请选择就诊卡"
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            hos:RunData.hos
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    choiceHos: function (item) {
        if (item.detail.value==-1 ||!this.data.hos||this.data.hos.length <= item.detail.value) {
            return
        }
        if (item.type === "change") {
            this.setData({
                choiced: item.detail.value,
                choicedHos: this.data.hos[item.detail.value].hospitalName,
                cards:undefined,
                choiceCardDisable:true,
                choicedCardIndex:-1,
                choicedCardTxt:"请选择就诊卡"
            });
            new getRegisterCardList(RunData.MyUserInfo.idNo,this.data.hos[item.detail.value].hospitalCode,RunData.MyUserInfo.idNo).http((data)=>{
                this.setData({
                    cards: data.body,
                    choiceCardDisable:false
                });
            });
        }
    },
    choiceCord:function (item) {
        if(!this.data.hos){
            wx.showToast({title: '请先选择医院', icon: 'none'});
            return
        }
        if (item.detail.value==-1||!this.data.cards || this.data.cards.length <= item.detail.value) {
            return
        }
        if (item.type === "change") {
            this.setData({
                choicedCardIndex: item.detail.value,
                choicedCardTxt: this.data.cards[item.detail.value].cardNo
            })
        }
    },
    quary: function () {
        if (!RunData.MyUserInfo) {
            wx.showToast({title: '请先登录', icon: 'none'});
            return
        }
        if (!this.data.hos[this.data.choiced] || !this.data.hos[this.data.choiced].hospitalCode) {
            wx.showToast({title: '请先选择医院', icon: 'none'});
            return
        }
        if (!this.data.cards[this.data.choicedCardIndex] || !this.data.cards[this.data.choicedCardIndex].cardNo) {
            wx.showToast({title: '请先选择就诊卡', icon: 'none'});
            return
        }
        wx.navigateTo({
            url: ('../myPayment/myPayment')
        });
        //todo 将参数传过去，然后在新页面调用接口
        // wx.showLoading({
        //     title: '查询中',
        // });
        // new getPrenosInfo(
        //     this.data.hos[this.data.choiced].hospitalCode,
        //     "",
        //     RunData.MyUserInfo.idNo,
        //     this.data.cards[this.data.choicedCardIndex].cardNo,
        //     this.data.cards[this.data.choicedCardIndex].cardNoType,
        //     "0"
        //     ).http((data)=>{
        //     console.log(data.unPaymentBody);
        // });
    }
});